package com.uyii.springbootgeodesy;

import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GeospatialController {

    @Autowired
    private GeospatialService geospatialService;

    /**
     * 计算两点之间的距离
     *
     * @param latitude1
     * @param longitude1
     * @param latitude2
     * @param longitude2
     */
    @GetMapping("/distance")
    public double getDistance(@RequestParam double latitude1,
                              @RequestParam double longitude1,
                              @RequestParam double latitude2,
                              @RequestParam double longitude2) {
        return geospatialService.calculateDistance(latitude1, longitude1, latitude2, longitude2);
    }

    /**
     * 计算两点之间的方位角
     *
     * @param latitude1
     * @param longitude1
     * @param latitude2
     * @param longitude2
     */
    @GetMapping("/azimuth")
    public double getAzimuth(@RequestParam double latitude1,
                             @RequestParam double longitude1,
                             @RequestParam double latitude2,
                             @RequestParam double longitude2) {
        return geospatialService.calculateAzimuth(latitude1, longitude1, latitude2, longitude2);
    }

    /**
     * 计算两点之间的航行时间
     *
     * @param latitude1
     * @param longitude1
     * @param latitude2
     * @param longitude2
     * @param speed
     */
    @GetMapping("/eta")
    public double calculateETA(
            @RequestParam double latitude1,
            @RequestParam double longitude1,
            @RequestParam double latitude2,
            @RequestParam double longitude2,
            @RequestParam double speed) {
        return geospatialService.calculateETA(latitude1, longitude1, latitude2, longitude2, speed);
    }

    /**
     * 计算地理边界
     *
     * @param latitude
     * @param longitude
     * @param distanceInMeters
     */
    @GetMapping("/bounding-box")
    public BoundingBox calculateBoundingBox(
            @RequestParam double latitude,
            @RequestParam double longitude,
            @RequestParam double distanceInMeters) {
        return geospatialService.calculateBoundingBox(latitude, longitude, distanceInMeters);
    }

    /**
     * 判定点是否在多边形内
     *
     * @param latitude
     * @param longitude
     * @param polygon
     */
    @PostMapping("/point-in-polygon")
    public boolean isPointInPolygon(
            @RequestParam double latitude,
            @RequestParam double longitude,
            @RequestBody List<GlobalCoordinates> polygon) {
        return geospatialService.isPointInPolygon(latitude, longitude, polygon);
    }

    /**
     * 计算点的平移
     *
     * @param latitude
     * @param longitude
     * @param distanceInMeters
     * @param bearing
     */
    @GetMapping("/translate-point")
    public GlobalCoordinates translatePoint(
            @RequestParam double latitude,
            @RequestParam double longitude,
            @RequestParam double distanceInMeters,
            @RequestParam double bearing) {
    return geospatialService.translatePoint(latitude, longitude, distanceInMeters, bearing);
    }

    /**
     * 计算多边形的面积
     *
     * @param polygon
     */
    @PostMapping("/polygon-area")
    public double calculatePolygonArea(@RequestBody List<GlobalCoordinates> polygon) {
        return geospatialService.calculatePolygonArea(polygon);
    }

}